#!/bin/bash

export LFS="/mnt/lfs"
export PREFIX="/tools"

function change_ownership(){
    chown -R root:root ${LFS}/${PREFIX}
}


function main(){
    change_ownership

    mkdir -v ${LFS}/{dev,proc,sys}

    mknod -m 600 ${LFS}/dev/console c 5 1
    mknod -m 666 ${LFS}/dev/null c 1 3

    mount -v --bind /dev ${LFS}/dev

    mount -vt devpts devpts ${LFS}/dev/pts
    mount -vt tmpfs shm ${LFS}/dev/shm
    mount -vt proc proc ${LFS}/proc
    mount -vt sysfs sysfs ${LFS}/sys

    chroot "${LFS}" ${PREFIX}/bin/env -i HONME=/root TERM="$TERM" PS1="\u:\w\$ " PATH="/bin:/usr/bin:/sbin:${PREFIX}/bin" ${PREFIX}/bin/bash --login +h

    mkdir -pv /{bin,boot,etc/opt,home,lib,mnt,opt}
    mkdir -pv /{media}/{floppy,cdrom},sbin,srv,var}
    install -dv -m 0750 /root
    install -dv -m 1777 /tmp /var/tmp
    mkdir -pv /usr/{,local/}{bin,include,lib,sbin,src}
    mkdir -pv /usr/{,local/}share/{doc,info,locale,man}
    mkdir -v /usr/{,local/}share/{misc,terminfo,zoneinfo}
    mkdir -pv /usr/{,local/}share/man/man{1..8}
    for dir in /usr /usr/local; do
        ln -sv share/{man,doc,info} $dir
    done
    case $(uname -m) in 
        x86_64) ln -sv lib /lib64 && ln -sv lib /usr/lib64 ;;
    esac
    mkdir -v /var/{lock,log,mail,run,spool}
    mkdir -pv /var/{opt,cache,lib/{misc,locate},local}

    ln -sv /tools/bin/{bash,cat,echo,pwd,stty} /bin
    ln -sv /tools/bin/perl /usr/bin
    ln -sv /tools/lib/libgcc_s.so{,.1} /usr/lib
    ln -sv /tools/lib/libstdc++.so{,.6} /usr/lib
    sed 's/tools/usr/' /tools/lib/libstdc++.la > /usr/lib/libstdc++.la
    ln -sv bash /bin/sh
    touch /etc/mtab

    cat > /etc/passwd << "EOF"
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/dev/null:/bin/false
nobody:x:99:99:Unprivileged User:/dev/null:/bin/false
EOF
    
    cat > /etc/group << "EOF"
root:x:0:
bin:x:1:
sys:x:2:
kmem:x:3:
tty:x:4:
tape:x:5:
daemon:x:6:
floppy:x:7:
disk:x:8:
lp:x:9:
dialout:x:10:
audio:x:11:
video:x:12:
utmp:x:13:
usb:x:14:
cdrom:x:15:
mail:x:34:
nogroup:x:99:
EOF

    exec ${PREFIX}/bin/bash --login +h
    touch /var/run/utmp /var/log/{btmp,lastlog,wtmp}
    chgrp -v utmp /var/run/utmp /var/log/lastlog
    chmod -v 664 /var/run/utmp /var/log/lastlog
    chmod -v 600 /var/log/btmp
}
main "$@"
